import{HTTP} from '../util/network.js'
import {
  obj_category, obj_goods, obj_address, obj_coupon, obj_discountItem, obj_goodsDetail,  obj_cartGoodsItem,obj_goodsOptionItem, obj_goodsSizeItem,obj_goodsIngredientsItem, obj_orderItem, obj_page} from '../util/model.js'


import {configLink} from '../util/config.js'

class RequestManager extends HTTP{
  //  获取首页分类
  getCategoryItemList(callback){
    this.doGetRequest(configLink.getKind, "", function (tmpItems){
      var categoryItems = [];
      tmpItems.forEach((tmpItem) => {
        var tmpCategoryItem = new obj_category(tmpItem);
        categoryItems.push(tmpCategoryItem);
      });
      callback(categoryItems);
    },function(err){

    })
  }
  //  获取某个分类下的商品
  getGoodsItemList(categoryItem, pageIndex,callback){
    this.doGetRequest(configLink.getGoodsByKind, { "kindid": categoryItem.id,"page":pageIndex,"pagesize":"10"}, function (result) {

      let pageItem = new obj_page(result);
      callback(pageItem);

      // var goodsItems = [];
      // tmpItems.forEach((tmpItem) => {
      //   var tmpGoodsItem = new obj_goods(tmpItem);
      //   goodsItems.push(tmpGoodsItem);
      // });
      // callback(goodsItems);
    })
  }
  //  获取商品详情
  getGoodsDetail(goodsId,callback){
    this.doGetRequest(configLink.getGoodsDetail,{"id":goodsId},function(result){
      if(result instanceof Array){
        var goodsDetailItem = new obj_goods(result[0]);
        callback(goodsDetailItem);
      }
    })
  }
  //  添加到购物车
  addGoodsItemToCart(openid,goodsId,tasteId,sizeId,callback){
    this.doGetRequest(configLink.addCart, { "openid": openid,"goodid":goodsId,"tasteid":tasteId,"sizeid":sizeId},function(result){
      console.log(result)
    })
  }
  //  更新购物车中商品的数量
  updateGoodsItemToCart(openid, itemId, count,callback){
    this.doGetRequest(configLink.updateCart, { "openid": openid, "id": itemId,"count":count}, result=>{
      console.log(result)
    })
  }
  //  购物车列表
  getAllCartList(openid,callback){
    this.doGetRequest(configLink.getAllCart, { "openid": openid},result=>{
      if (result instanceof Array) {
        var cartGoodItems = [];

        result.forEach((tmpItem) => {

          let goodJson = tmpItem.good;
          let sizeJson = tmpItem.size;
          let tasteJson = tmpItem.taste;

          let count = tmpItem.quantity

          if (goodJson != null){
            let tmpGoodItem = new obj_goods(goodJson);
            let tmpOptionItem = new obj_goodsOptionItem(tasteJson);
            let tmpSizeItem = new obj_goodsSizeItem(sizeJson);

            var tmpCartGoodItem = new obj_cartGoodsItem(tmpGoodItem, tmpOptionItem, tmpSizeItem, count)
            tmpCartGoodItem.id = tmpItem.id;
            cartGoodItems.push(tmpCartGoodItem);
          }
        });
        callback(cartGoodItems);
      }
    })
  }
  //  删除购物车
  removeGoodsItemFromCart(openid, itemIds,callback){
    this.doGetRequest(configLink.delCart, { "openid": openid,"id": itemIds}, success => {
      console.log("删除购物车：", success)
    })
  }
  //  获取用户信息
  getUserInfo(callback){
  }
  //  绑定优惠券
  bindUserCouponItem(openid,couponkey,couponsecret,callback){
    this.doGetRequest(configLink.bindCoupon, { "openid": openid, "couponkey": couponkey, "couponsecret": couponsecret},resultItem=>{
      callback();
    },faile=>{
      wx.showToast({
        title: faile,
        icon:'none'
      })
    })
  }
  //  获取用户优惠券列表
  getUserCouponList(openid,callback){
    this.doGetRequest(configLink.myCoupon, { "openid": openid }, resultItems => {
      if (resultItems instanceof Array){
        var couponItems = [];
        resultItems.forEach((tmpItem) => {
          var tmpCouponItem = new obj_coupon(tmpItem);
          couponItems.push(tmpCouponItem);
        });
        callback(couponItems);
      }
    })
  }
  //  获取用户收货地址列表
  getUserAddressList(openid,callback){
    this.doGetRequest(configLink.getAllAddressList, { "openid": openid}, resultItems => {
      console.log("收货地址列表：", resultItems)
      if (resultItems instanceof Array) {
        var addressItems = [];
        resultItems.forEach((tmpItem) => {
          var tmpAddressItem = new obj_address(tmpItem);
          addressItems.push(tmpAddressItem);
        });
        callback(addressItems);
      }
    })
  }
  //  新增收货地址
  addAddressItemToWeb(openid,name,province,city,address,phone,callback){
    this.doGetRequest(configLink.addAddress, { "openid": openid, "name": name, "province": province, "city": city, "address": address, "phone": phone }, success => {
      callback(success)
    }, failedMsg => {
      wx.showToast({
        title: failedMsg,
        icon: 'none'
      })
    })
  }
  //  新增订单
  addOrderItemToWeb(openid, deliveryfee, couponid, addressid, goodlist, bookdate, bookenddate,remark,callback){

    var parameters = { "openid": openid, " deliveryfee": deliveryfee, "addressid": addressid, "goodlist": goodlist, "bookdate": bookdate, "bookenddate": bookenddate}    
    if(couponid != -1){
      //  优惠券id为-1则表示不使用优惠券
      parameters["couponid"] = couponid
    }
    if(remark != null){
      parameters["memo"] = remark
    }

    this.doGetRequest(configLink.commitOrder, parameters,success=>{
      callback(success)
    }, failedMsg=>{
      wx.showToast({
        title: failedMsg,
        icon:'none'
      })
    })
  }
  //  支付订单
  payOrderItem(openid,orderid,callback){
    var parameters = { "openid": openid, "orderid": orderid}
    this.doGetRequest(configLink.payOrder, parameters, success => {
      callback(success)
    }, failedMsg => {
      wx.showToast({
        title: failedMsg,
        icon: 'none'
      })
    })
  }
  //  获取用户订单列表
  getUserOrderList(openid,callback){
    this.doGetRequest(configLink.myOrder, { "openid": openid}, resultItems => {
      console.log("获取用户订单列表：", resultItems)

      var tmpOrderItems = []

      if (resultItems instanceof Array) {
        resultItems.forEach(tmpItem=>{
          var tmpOrder = new obj_orderItem(tmpItem)
          //  此处需要处理下订单状态，只显示出已支付和已完成的订单
          if (tmpOrder.orderType == 1 || tmpOrder.orderType == 2){
            tmpOrderItems.push(tmpOrder)
          }
        })
      }
      callback(tmpOrderItems);
    })
  }
  //  取消订单
  cancelOrder(openid,orderid,callback){
    this.doGetRequest(configLink.cancelOrder, { "openid": openid,"orderid":orderid }, resultItem => {
      callback();
    })
  }
  //  用户注册
  userRegister(openid,nickname,head, callback){
    this.doGetRequest(configLink.uploadUserInfo, { "openid": openid, "nickname": nickname, "head": head},result=>{

    })
  }
  //  获取OpenId
  getUserOpenId(code,callback){
    this.doGetRequest(configLink.getOpenId, { "code": code }, function (result) {
      callback(result)
    })
  }
    //  绑定分销者的openid
  bindParentOpenId(openid,parentId,callback){
    this.doGetRequest(configLink.bindOpenid, { "openid": openid,"parent_openid":parentId},result=>{
      console.log("绑定分销者的openid结果",result)
    })
  }
}

export {
  RequestManager
}